<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class OrderController extends Controller
{
    /**
     * 店铺未发货订单显示
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        //
        $id = session()->get('sname')->id;
        $keywords = $request->input('keywords');
        if(empty($keywords)){
            $order = DB::table('orders')
                ->leftJoin('goods','goods.id','=','orders.goods_id')->where('shop_id',$id)
                ->leftJoin('address','orders.addr_id','=','address.id')->where('oStatus','<','2')->get();
        }else{
            $order = DB::table('orders')
                ->leftJoin('goods','goods.id','=','orders.goods_id')->where('shop_id',$id)
                ->leftJoin('address','orders.addr_id','=','address.id')->where('oStatus','<','2')->where('orders.onum','like','%'.$keywords.'%')->get();
        }
        return view('shop.order.index',['order'=>$order]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getShow($id)
    {
        //
        $data = DB::table('orders')
            ->leftJoin('goods','goods.id','=','orders.goods_id')
            ->leftJoin('address','orders.addr_id','=','address.id')->where('onum','=',$id)->get()[0];
        // dd($data);
        return view('shop.order.show',['data'=>$data]);
    } 

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getUpdate($onum,$id)
    {
        $data['oStatus']=$id;
        $res = DB::table('orders')->where('onum','=',$onum)->update($data);
        if($res){
            return redirect('/sporder/onindex');
        }else{
            return back();
        }
    }
    /**
     * 店铺已发货订单显示
     * @return \Illuminate\Http\Response
     */
    public function getOnindex(Request $request)
    {
        //
        $id = session()->get('sname')->id;
        $keywords = $request->input('keywords');
        if(empty($keywords)){
            $order = DB::table('orders')
                ->leftJoin('goods','goods.id','=','orders.goods_id')->where('shop_id',$id)
                ->leftJoin('address','orders.addr_id','=','address.id')->where('oStatus','>=','2')->get();
        }else{
            $order = DB::table('orders')
                ->leftJoin('goods','goods.id','=','orders.goods_id')->where('shop_id',$id)
                ->leftJoin('address','orders.addr_id','=','address.id')->where('oStatus','>=','2')->where('orders.onum','like','%'.$keywords.'%')->get();
        }
        return view('shop.order.onindex',['order'=>$order]);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
